// pages/add-address/index.js
import Config from '../../etc/config'
const { $Toast } = require('../../components/dist/base/index');
var app = getApp()
Page({
  data: {
    isAgree: true, // 是否设置为默认地址
    id:'',
    user_name: '',
    user_tel: '',
    user_address: '',
    user_address_info: '',
    user_address_num:'',
    disabled:false
  },

  // 是否为默认地址
  bindAgreeChange: function(e) {
    this.setData({
      isAgree: !this.data.isAgree
    });
  },

  onLoad: function(e) {
    if(e.id){
      wx.setNavigationBarTitle({
        title: '编辑地址'
      })
      this.getAddrById(e.id)
    }else{
      wx.setNavigationBarTitle({
        title: '添加地址'
      })
    }
    wx.hideShareMenu()
  },

  submit(e){
    this.setData({
      disabled:true
    })
    if (!e.detail.value.userName){
      $Toast({
        content: '请输入姓名'
      })
      this.setData({
        disabled: false
      })
      wx.hideLoading();
      return
    }
    if (!e.detail.value.telNumber) {
      $Toast({
        content: '请输入手机号码'
      })
      this.setData({
        disabled: false
      })
      wx.hideLoading();
      return
    }
    if (!this.data.user_address_info){
      $Toast({
        content: '请选择地址'
      })
      this.setData({
        disabled: false
      })
      wx.hideLoading();
      return
    }
    let addressInfo = {
      userId: wx.getStorageSync("userId"),
      userName: e.detail.value.userName,
      telNumber: e.detail.value.telNumber,
      member: e.detail.value.cardNumber,
      userLongitude: this.data.userLongitude,
      userLatitude: this.data.userLatitude,
      detailInfo: this.data.user_address_info,
      doorPlate: e.detail.value.user_address_num,
      isDefault: this.data.isAgree ? 1 : 0
    }
    let token = wx.getStorageSync("token");
    if(this.data.id){
      addressInfo.id = this.data.id,
      this.editAddress(addressInfo, token)
    }else{
      this.saveNewsAddress(addressInfo,token)
    }
  },
  // 保存地址
  saveNewsAddress(addressInfo, token) {
    var that = this
    wx.showLoading({
      title: '正在保存',
      success: function(data) {
        Config._post('/useraddress/saveUserAddress', token, addressInfo).then(res =>{
          that.setData({
            disabled:false
          })
          wx.hideLoading();
          if(res.code === 0){
            wx.navigateBack();
          }else{
            $Toast({
              content: res.msg
            })
          }
        })
      }
    })
  },

  // 修改地址
  editAddress(addressInfo, token){
    var that = this
    wx.showLoading({
      title: '正在保存',
      success: function () {
        Config._post('/useraddress/updateUserAddress', token, addressInfo).then(res => {
          that.setData({
            disabled: false
          })
          wx.hideLoading();
          if (res.code === 0) {
            wx.navigateBack();
          } else {
            $Toast({
              content: res.msg
            })
          }
        })
      }
    })
  },

  // 选择地址
  chooseAddress(){
    var that = this
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userLocation'] === false) {
          wx.openSetting({})
        }else{
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              // 用户已经同意小程序获取地理位置，后续调用 wx.chooseLocation 接口不会弹窗询问
              wx.chooseLocation({
                success(res) {
                  that.setData({
                    user_address_info: res.address + res.name,
                    userLatitude: res.latitude,
                    userLongitude: res.longitude
                  })
                },
                fail(res) {
                  // console.log('错误：', res)
                }
              })
            }
          })
        }
      }
    })
  },

  // 根据地址ID获取地址详情
  getAddrById(val){
    let params={
      id: val
    }
    let token = wx.getStorageSync("token");
      Config._post('/useraddress/getAddrById',token,params).then(res=>{
        this.setData({
          id: res.data.id,
          user_name: res.data.userName,
          user_tel: res.data.telNumber,
          user_address_info: res.data.detailInfo,
          user_address_num: res.data.doorPlate,
          cardNumber: res.data.member,
          isAgree: res.data.isDefault===0?false:true,
          userLatitude: res.data.userLatitude,
          userLongitude: res.data.userLongitude
        })
      })
  }
})